package cm367;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import java.util.Date;

@ManagedBean
@ApplicationScoped
public class Booking {

	private List<BookingDetail> booking = new ArrayList<BookingDetail>();

	@ManagedProperty(value = "#{venueService}")
	private VenueService venueservice;

	private boolean display;

	public Booking() throws IOException {
		DataConnection d = new DataConnection();
		List<String> tmp = d.getTable("db_booking.txt");

		BookingDetail v = null;
		for (int i = 1; i < tmp.size(); i++) {
			v = new BookingDetail(tmp.get(i));
			booking.add(v);
		}
	}

	public List<BookingDetail> getBookinglist() {
		try {
			new Booking();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return booking;
	}

	public boolean isDisplay() {
		if (getBooking().size() > 0)
			return true;
		else
			return false;
	}

	public void setDisplay(boolean display) {
		this.display = display;
	}

	public List<BookingDetail> getBooking() {
		List<BookingDetail> tmp_book = new ArrayList<BookingDetail>();
		Date tmp_date;
		if (venueservice.getDate() != null) {
			for (int i = 0; i < booking.size(); i++) {
				tmp_date = (booking.get(i).getBookDate()).getTime();
				if (tmp_date.getYear() == (venueservice.getDate()).getYear()
						&& tmp_date.getMonth() == (venueservice.getDate())
								.getMonth()
						&& tmp_date.getDate() == (venueservice.getDate())
								.getDate())
					tmp_book.add(booking.get(i));
			}
		}
		return tmp_book;

	}

	public VenueService getVenueservice() {
		return venueservice;
	}

	public void setVenueservice(VenueService venueservice) {
		this.venueservice = venueservice;
	}

}
